package rbk.rours.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import rbk.rours.common.dto.MinioDto;
import rbk.rours.common.utils.JsonResult;
import rbk.rours.common.utils.minio.MinioUtil;

@RestController
public class MinioController {
    @Autowired
    private MinioUtil minioUtil;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult Upload(@RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(required = false) String bucketName) {
        try {
            return minioUtil.UploadFile(file, bucketName);
        } catch (Exception ex) {
            return JsonResult.errorMsg(ex.getMessage());
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResult Delete(@RequestBody MinioDto request) {
        try {
            minioUtil.RemoveObject(request.getBucketName(), request.getObjName());
            return JsonResult.ok();
        } catch (Exception ex) {
            return JsonResult.errorMsg(ex.getMessage());
        }
    }
}
